package cn.bdqn.pn.controller.user;

import cn.bdqn.pn.pojo.user.Resource;
import cn.bdqn.pn.pojo.user.Role;
import cn.bdqn.pn.service.user.ResourceService;
import cn.bdqn.pn.service.user.RoleService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/Resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private RoleService roleService;
    @RequestMapping(value = "/selectResource")
    public String selectResource(Model model, @RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        Page<Resource> resourcePage=null;
        resourcePage=resourceService.selectResource(pageNo,pageSize);
        model.addAttribute("page",resourcePage);
        /*Page<User> datas=userService.selectUserInfo(page,size);
        List<User> userList=datas.getContent();
        for (User user:userList
             ) {
            System.out.println(datas);
        }*/
        return "Resource_list";
    }
    @RequestMapping(value = "/addResource")
    public String toAdd(Resource resource){
        return "addResource";
    }

    @RequestMapping("/add")
    public String add(Resource resource) {
        resourceService.addResource(resource);
        return "redirect:selectResource";
    }

    @RequestMapping(value = "/updateResource")
    public String update(Model model, Integer resourceid) {
        Resource resource=resourceService.findResourceById(resourceid);
        model.addAttribute("resource", resource);
        return "updateResource";
    }

    @RequestMapping("/update")
    String updateRole(Resource resource){
        resourceService.updateResource(resource);
        return "redirect:selectResource";
    }
    @RequestMapping(value = "/deleteResource")
    String deleteRole(Integer resourceid ){
        resourceService.deleteResource(resourceid);
        return "redirect:selectResource";
    }

    @RequestMapping(value = "selectAll")
    String selectAll(Model model){
        List<Role> listRole=new ArrayList<>();
        List<Resource> list=new ArrayList<>();
        list=resourceService.selectAll();
        listRole=roleService.selectAllRole();
        model.addAttribute("allResource",list);
        System.out.println(JSON.toJSONString(resourceService.selectAll(),true));
        model.addAttribute("allRole",listRole);
        return "Resource";
    }
}
